<template>
	<view class="link">
		<view class="colleagues-name">
			企业名称：
			<view class="uni-input colleagues-name">
				{{companyname}}
			</view>
			
		</view>
		
		<view class="colleagues-name">
			企业部门：
			<view class="uni-input colleagues-name">
				{{departname}}
			</view>
		</view>
		
		<view class="colleagues-name">
			绑定状态：
			<view class="uni-input colleagues-name">
				{{is_m=='is_m'?'部门经理':'普通员工'}}
			</view>
		</view>
		
		<view class="pT10" v-if="!loginbtn">
				<p class="f12 tCenter">清注册个人信息，以便加入公司！</p>
		</view>
		<!-- <view class="position">
			职位：
			<input class="uni-input position" name="input" disabled placeholder="请填写部门名称" />
		</view> -->
		<view class="bnt_all" v-if="loginbtn"><view class="bnt-add" :class="{control:controlchanger}" @click="adduser">确定</view></view>
		<view class="bnt_all" v-if="!loginbtn">
			
			<view class="bnt-add"  @click="gologinshow">去注册</view>
			</view>
		<backhome v-if="!showbackindex"></backhome>
		<!-- 加载动画 -->
		<loading v-if="!isloading()"></loading>
	</view>
</template>

<script>
	import {deptuseradd,deptinfo} from '../../common/api/department.js'; //引进requst方法
	import backhome from '@/components/backhome.vue';
	import {questiontrain} from '@/common/api/topics.js'
	import {companyapply,applyapyinfo} from '@/common/api/company.js'	
	import {userinfo} from '@/common/api/user.js'
	export default {
		components: {
			backhome
		},
		data() {
			return {
				loginbtn:true,
				showbackindex:true,
				controlchanger:true,
				onecheckup:true, //第一次加入
				companyid:"",
				detaildata:[],
				id:"",
				departname:"",
				companyname:"",
				uinfo:'',
				is_m:"",
				showclick:true,
				identitycard:"",
			};
		},
		methods: {
			gologinshow(){
				 this.goshouji();
				
			},
			adduser(){
		// if(this.identitycard){	
			console.log('identitycard'+this.identitycard)
				let that=this;
				if(this.isgologin()){
				if(this.uinfo.dept_name){				
					uni.showModal({
						title: '',
						content: '您当前您隶属于'+this.uinfo.company_name+',无法加入其它公司部门',						
						 showCancel:false,
						confirmText: "确定",
						success: function (res) {
							if (res.confirm) {
								that.showbackindex=false;
								that.controlchanger=true;
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					
					return;
				}
				if(!this.onecheckup){
					uni.showModal({
						title: '',
						content: '您已提交过申请加入部门，不能重复提交',						
						 showCancel:false,
						confirmText: "确定",
						success: function (res) {
							if (res.confirm) {
								that.showbackindex=false;
								that.controlchanger=true;
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					
					return;
				}
				if(this.showclick){
					this.showclick=false;
				// let userid=uni.getStorageSync('userid');
				let userid = this.uinfo.id; 
				companyapply({deptid:this.id,uid:userid,companyid:this.companyid,is_m:this.is_m}).then(res=>{
					console.log(res)
					this.showclick=true;
					this.showbackindex=false;
					if(res.data.code == 200){
						uni.showModal({
							title: '',
							content: '成功加入部门,请等待管理员审核',						
							 showCancel:false,
							confirmText: "确定",
							success: function (res) {
								if (res.confirm) {
									questiontrain({
										companyid:that.companyid,
										checktranin:'check',
									}).then(rese=>{
										console.log(rese)
										if(rese.data.status == 0){
											let details = {
												companyid:that.companyid,
												type:'dep'						
											}
											uni.reLaunch({
												url:"../subject/problem?details="+encodeURIComponent(JSON.stringify(details))
											});
										}else{
											uni.reLaunch({
												url:"../user/user?details="+encodeURIComponent(JSON.stringify(details))
											});
										}
									}); 
									
									
									
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
						
					}else{
						
						// uni.showToast({
						//     title: res.data.message,
						//     duration: 2000,
						// 	icon:'none'
						// });
						uni.showModal({
							title: '',
							content: res.data.message,						
							 showCancel:false,
							confirmText: "确定",
							success: function (res) {
								if (res.confirm) {
									that.controlchanger=true;
									uni.reLaunch({
										url:"../user/user?details="+encodeURIComponent(JSON.stringify(details))
									});
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
				})
				}
			}
		// }else{
		// 	uni.showModal({
		// 		title: '',
		// 		content: '请先进行实名认证',						
		// 		 showCancel:false,
		// 		confirmText: "确定",
		// 		success: function (res) {
		// 			if (res.confirm) {
		// 			} else if (res.cancel) {
		// 				console.log('用户点击取消');
		// 			}
		// 		}
		// 	});
		// }	
			},
		},
		onLoad(event) {	
			let that=this;
			console.log("aaa",event);
			let eventt =JSON.parse(event.detailDate);
			console.log(eventt);
			that.id = eventt.deptid;
			console.log("deptid",that.id);
			that.is_m=eventt.is_m;
			that.departname=eventt.departname;
			that.companyid=eventt.companyid;
			deptinfo({id:that.id}).then(res=>{
				console.log(res)
				that.companyid =res.data.data.companyid
				that.companyname=res.data.data.name;	
				that.departname=res.data.data.dept_name;				
			})
			if(that.islogin()){
				that.loginbtn=true;
				that.controlchanger=false;
				userinfo().then(res=>{			
					that.uinfo = res.data.data
					console.log('uinfo'+that.uinfo);
					that.identitycard=res.data.data.identitycard;
					if(that.uinfo.companyid){
						that.controlchanger=true;
					}
					uni.setStorage({
						key: 'userInfo',
						data: res.data
					});
				});
				applyapyinfo({deptid:that.id}).then(res=>{					
					if(res.data.data.status){
						that.onecheckup=false;
						that.controlchanger=true;
					}else{
						that.onecheckup=true;
					}
					
				});
				
				
			}else{
				that.loginbtn=false;
			}
			
		},
	}
</script>

<style lang="scss">
.link {
	width: 100%;
	background-color: white;
	.colleagues-name {
		color: rgba(80, 80, 80, 1);
		font-size: 15px;
		display: flex;
		align-items: center;
		width: 95%;
		margin: auto;
		padding: 10px 0;
		border-bottom: 1px solid #eee;
	}
	.position {
		color: rgba(80, 80, 80, 1);
		font-size: 15px;
		display: flex;
		align-items: center;
		width: 95%;
		margin: auto;
		padding: 5px 0;
		border-bottom: 1px solid #eee;
	}
	.uni-input {
		border: none;
	}
	.bnt_all {
		width: 100%;		
		text-align: center;
		margin: auto;
		position: absolute;
		bottom: 50px;
		 left: 0%;
	}
	.bnt-add{
		width: 192px;
		height: 42px;
		color: rgba(255, 255, 255, 1);
		background-color: rgba(42, 130, 228, 1);
		border-radius: 18px;
		font-size: 15px;
		line-height: 42px;
		text-align: center;
		margin: auto;
		// left: 27%;
	}
	.control{
		background:#5f5f5f;
	}
}


</style>